<?php
namespace Hive\Ressource\Wish;
/**
 * Wished Localization
 * To maintain where the contact want to work
 * 
 * @author Xavier VILAIN
 * @package Hive\Ressource\Wish
 * 
 * @property int 	$did			Wish Id
 * @property int 	$ressource_id	Ressource Id
 * @property string	$code			Localization Code ([x]_[id]) where x is the localization
 * level s,g,r and [id] localization id at specified level
 * @property int	$type 			Localization Level 1,2,3
 * @property int 	$id				Localization id
 */
class Localization extends \Fox\Object {
	
	/**
	 * Link to wsih
	 * @var string
	 */
	const LINK_WISH = 'wish';
	
	/**
	 * Group Level 
	 * @var int
	 */
	const LEVEL_GROUP = 1;
	
	/**
	 * Region Level 
	 * @var int
	 */
	const LEVEL_REGION = 2;
	
	/**
	 * Sub Region Level 
	 * @var int
	 */
	const LEVEL_SUBREGION = 3;
	
	/**
	 * 
	 * Explode Localization Tokens into an 
	 * array 
	 * @param unknown_type $jsonTokens
	 */
	static function parseTokens($jsonTokens = null) {
		if(is_array($jsonTokens)) {
			return array();
		}
		$data = json_decode(preg_replace("/\'/",'"',$jsonTokens),true);
		$localization = array();
		if(is_array($data)) {
			foreach($data as $item) {
				$key = substr($item['id'],0,1);
				$id = substr($item['id'],2);
				
				switch ($key) {
					case 'g' :
						$localization[] = array('id' => $id,'level' => self::LEVEL_GROUP,'code' => $item['id']);
						break;
					case 'r' :
						$localization[] = array('id' => $id,'level' => self::LEVEL_REGION,'code' => $item['id']);
						break;
					case 's' : 
						$localization[] = array('id' => $id,'level' => self::LEVEL_SUBREGION,'code' => $item['id']);
						break;
				}
			}
		}
		return $localization;
	}
}
\Fox\Dao::infos(Localization::className(),new \Fox\Object\Info('ressource_wish_localizations', \Fox\Databases::DB_APP));